package com.yueqianshop.order.web;

import com.alipay.api.AlipayApiException;
import com.yueqianshop.common.utils.PageUtils;
import com.yueqianshop.order.config.AlipayTemplate;
import com.yueqianshop.order.service.OrderService;
import com.yueqianshop.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.HashMap;

@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;

    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        PayVo payVo = orderService.payOrder(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);
        return pay;
    }

    @GetMapping("orderList.html")
    public String orderListPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model){
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", pageNum.toString());
        PageUtils page = orderService.queryPageWithItem(params);
        model.addAttribute("orders", page.getList());
        return "list";
    }
}
